﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using SAPbobsCOM;
namespace ApexInterest
{   
    public class Function
    {
        private static SAPbobsCOM.Company m_Company;
        public static SAPbobsCOM.Company Company
        {
            get { return m_Company; }
            set { m_Company = value; }
        }
        public static CultureInfo GetCulture(SAPbouiCOM.Application Application, SAPbobsCOM.Company oCompany)
        {
            AdminInfo adminInfo = oCompany.GetCompanyService().GetAdminInfo();

            string strCultureName;

            switch (Application.Language)
            {
                case SAPbouiCOM.BoLanguages.ln_Hebrew:
                    strCultureName = "he";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Spanish_Ar:
                    strCultureName = "es-AR";
                    break;
                case SAPbouiCOM.BoLanguages.ln_English:
                    strCultureName = "en-US";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Polish:
                    strCultureName = "pl";
                    break;
                case SAPbouiCOM.BoLanguages.ln_English_Sg:
                    strCultureName = "en-US";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Spanish_Pa:
                    strCultureName = "es-PA";
                    break;
                case SAPbouiCOM.BoLanguages.ln_English_Gb:
                    strCultureName = "en-GB";
                    break;
                case SAPbouiCOM.BoLanguages.ln_German:
                    strCultureName = "de";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Serbian:
                    strCultureName = "sr-SP-Cyrl";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Danish:
                    strCultureName = "da";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Norwegian:
                    strCultureName = "no";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Italian:
                    strCultureName = "it";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Hungarian:
                    strCultureName = "hu";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Chinese:
                    strCultureName = "zh-CN";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Dutch:
                    strCultureName = "nl";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Finnish:
                    strCultureName = "fi";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Greek:
                    strCultureName = "el";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Portuguese:
                    strCultureName = "pt";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Swedish:
                    strCultureName = "sv";
                    break;
                case SAPbouiCOM.BoLanguages.ln_English_Cy:
                    strCultureName = "en";
                    break;
                case SAPbouiCOM.BoLanguages.ln_French:
                    strCultureName = "fr";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Spanish:
                    strCultureName = "es";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Russian:
                    strCultureName = "ru";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Spanish_La:
                    strCultureName = "es";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Czech_Cz:
                    strCultureName = "cs";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Slovak_Sk:
                    strCultureName = "sk";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Korean_Kr:
                    strCultureName = "ko";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Portuguese_Br:
                    strCultureName = "pt-BR";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Japanese_Jp:
                    strCultureName = "ja";
                    break;
                case SAPbouiCOM.BoLanguages.ln_Turkish_Tr:
                    strCultureName = "tr";
                    break;
                case SAPbouiCOM.BoLanguages.ln_TrdtnlChinese_Hk:
                    strCultureName = "zh-HK";
                    break;
                default:
                    strCultureName = "en-US";
                    break;
            }

            CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture(strCultureName);

            cultureInfo.NumberFormat.CurrencyDecimalSeparator = adminInfo.DecimalSeparator;
            cultureInfo.NumberFormat.CurrencyGroupSeparator = adminInfo.ThousandsSeparator;
            cultureInfo.NumberFormat.NumberDecimalSeparator = adminInfo.DecimalSeparator;
            cultureInfo.NumberFormat.NumberGroupSeparator = adminInfo.ThousandsSeparator;

            switch (adminInfo.DateTemplate)
            {
                case BoDateTemplate.dt_DDMMYY:
                    cultureInfo.DateTimeFormat.ShortDatePattern = string.Format("dd{0}MM{0}yy", adminInfo.DateSeparator);
                    break;
                case BoDateTemplate.dt_DDMMCCYY:
                    cultureInfo.DateTimeFormat.ShortDatePattern = string.Format("dd{0}MM{0}yyyy", adminInfo.DateSeparator);
                    break;
                case BoDateTemplate.dt_MMDDYY:
                    cultureInfo.DateTimeFormat.ShortDatePattern = string.Format("MM{0}dd{0}yy", adminInfo.DateSeparator);
                    break;
                case BoDateTemplate.dt_MMDDCCYY:
                    cultureInfo.DateTimeFormat.ShortDatePattern = string.Format("MM{0}dd{0}yyyy", adminInfo.DateSeparator);
                    break;
                case BoDateTemplate.dt_DDMonthYYYY:
                    cultureInfo.DateTimeFormat.ShortDatePattern = string.Format("dd{0}MMMM{0}yyyy", adminInfo.DateSeparator);
                    break;
            }

            switch (adminInfo.TimeTemplate)
            {
                case BoTimeTemplate.tt_12H:
                    {
                        cultureInfo.DateTimeFormat.ShortTimePattern =
                            cultureInfo.DateTimeFormat.ShortTimePattern.
                            Replace("HH", "hh").Replace("H", "h");

                        cultureInfo.DateTimeFormat.LongTimePattern =
                            cultureInfo.DateTimeFormat.LongTimePattern.
                            Replace("HH", "hh").Replace("H", "h");
                    }
                    break;
                case BoTimeTemplate.tt_24H:
                    {
                        cultureInfo.DateTimeFormat.ShortTimePattern =
                            cultureInfo.DateTimeFormat.ShortTimePattern.
                            Replace("hh", "HH").Replace("h", "H");

                        cultureInfo.DateTimeFormat.LongTimePattern =
                            cultureInfo.DateTimeFormat.LongTimePattern.
                            Replace("hh", "HH").Replace("h", "H");
                    }
                    break;
            }

            return cultureInfo;
        }
    }
}
